package com.example.lib_rongrui.utils;

import android.widget.EditText;

public class Util {

	public static boolean isEtEmpty(EditText editText) {
		String str = editText.getText().toString();
		return str == null || str.isEmpty();
	}

	public static boolean isLenLegal(EditText editText) {
		String str = editText.getText().toString();
		return !isEtEmpty(editText) && str.length() % 2 == 0;
	}

	public static boolean isEtsLegal(EditText[] ets) {
		for (EditText et : ets) {
			if (isLenLegal(et)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isHexNumber(String str) {
		if (str == null || str.isEmpty()) {
			return false;
		}
		for (char cc : str.toCharArray()) {
			if (!Character.isDigit(cc) && (cc < 'A' || cc > 'F') && (cc < 'a' || cc > 'f')) {
				return false;
			}
		}
		return true;
	}

	public static String bytesToHexString(byte[] src, int offset, int length) {
		if (src == null || src.length == 0 || offset < 0 || length > src.length) {
			return null;
		}
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = offset; i < length; ++i) {
			String hv = Integer.toHexString(src[i] & 0xFF);
			if (hv.length() == 1) {
				stringBuilder.append('0');
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString().toUpperCase();
	}

	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.isEmpty()) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		byte[] d = new byte[length];
		for (int i = 0; i < length; ++i) {
			int pos = i * 2;
			d[i] = (byte) ((charToByte(hexString.charAt(pos)) << 4) | charToByte(hexString.charAt(pos + 1)));
		}
		return d;
	}

	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}
}